#/bin/bash
#exit
echo "********************************************"
echo "此脚本用于加载docker镜像服务"
echo "********************************************"

images=$1

function print(){
        echo -e "$2> $1 $3"
}
function print_error(){
        print "$1" '\e[31m' '\e[0m'
}
function print_success(){
        print "$1" '\e[34m' '\e[0m'
}

function load_image(){
  msg=`docker load -i images/$1.tar`

  if [ ! -f "images/$1.tar" ];then
     print_error "\t镜像不存在:images/$1.tar,安装脚本退出..."
     exit 1
  fi

  if [ "$?" != "0" ];then
     print_error "\t导入Docker镜像失败,安装脚本退出..."
     exit 1
  fi
  print "\t$mgs"
  print_success "\t导入Docker $1镜像成功！\n"
}

if [ -z "$images" ]; then
    echo "没有镜像需要加载"
    exit 1
else
    echo "当前需要加载的镜像:$images"
fi



#确认是否已经安装docker
de=`docker --version 2>/dev/null`
if [ "$?" -ne "0" ];then
        print_error "docker 未安装，请确认！ \n$de\n安装脚本退出..."
        exit 1;
fi

c_dir=$(pwd)

docker_status=`systemctl status docker|grep running`
if [ -z "$docker_status" ];then
	curr_uid=`id -u`
	if [ "0" = "$curr_uid" ];then
		systemctl start docker
	else
		print "docker 服务未启动，请使用root用户启动docker服务:\n\t systemctl start docker"
		print "安装脚本退出..."
		exit 1
	fi
fi

print "当前目录:$c_dir"
print "***导入Docker镜像文件..."


for image in ${images//,/ }
do
  load_image $image
done

docker images


print " 
=====================================================
	#启动
	cd $c_dir && sh startup.sh
=====================================================
"


